package com.westvalley.syn.service;

import java.util.Iterator;
import java.util.Map;

import weaver.hrm.job.JobActivitiesComInfo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.kit.SqlsKit;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.kit.SqlsKit.SqlOperateType;
import com.westvalley.common.result.SqlResult;
import com.westvalley.syn.bean.Result;
import com.westvalley.service.cache.HrJobActivitiesCache;
import com.westvalley.service.cache.HrJobGroupsCache;

/**
 * 职务信息同步
 * @author mos
 *
 */
public class HrJobActivitiesService extends SynService{
	private static final String KEY = "jobStd";
	private HrJobGroupsCache jobGroupsCache = HrJobGroupsCache.me();
	private HrJobActivitiesCache jobActivitiesCache = HrJobActivitiesCache.me();
	
	public HrJobActivitiesService(){
		refreshCache();
		this.url = prop.getPropValue(KEY);
	}
	
	@Override
	protected void initParams(Map<String, String> params) {
		params.put("shopId", "TSG1");
	}
	
	@Override
	protected Result synMainData(JSONArray jsonArrData) {
		Iterator<Object> it;
		if(jsonArrData!=null && (it=jsonArrData.iterator())!=null){
			SqlResult sqlResultInsert = SqlResult.me();
			SqlResult sqlResultUpdate = SqlResult.me();
			while (it.hasNext()) {
				JSONObject ob = (JSONObject) it.next();
				if( ob!=null ){
					String id = ob.getString("id");	//职位编码
					if( StrKit.notBlank(id) ){
						Record jobActivitiesRecord = this.jobActivitiesCache.getRecordByCode(id);	//缓存中职务信息
						if( jobActivitiesRecord==null ){
							sqlResultInsert.set(insertData(id,ob));
						}else{
							sqlResultUpdate.set(updateData(id,ob));
						}
					}else{
						return Result.me(new RuntimeException("数据主键为空！"));
					}
				}
			}
			int[] insertNums = sqlResultInsert.batch(500);
			int[] updateNums = sqlResultUpdate.batch(500);
			return Result.me(jsonArrData.size(),insertNums,updateNums);
		}else{
			return Result.me(new RuntimeException("接口数据为空无法同步！"));
		}
	}

	@Override
	protected Result synpOtherData(Result result, JSONArray jsonArrData) {
		return result;
	}

	@Override
	protected void refreshCache() {
		this.jobGroupsCache.reload();
		this.jobActivitiesCache.reload();
		try{new JobActivitiesComInfo().removeJobActivitiesCache();}catch (Exception e) {}
	}
	
	/**
	 * 新增数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit insertData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("HrmJobActivities");
		sqlsKit.addData("jobactivityname", StringKit.null2Str(ob.getString("name")));	//名称
		sqlsKit.addData("jobactivitymark", StringKit.null2Str(ob.getString("id")));		//编码
		sqlsKit.addData("jobgroupid", "83");											//固定使用1
		sqlsKit.setOperateType(SqlOperateType.INSERT);
		return sqlsKit;
	}
	
	/**
	 * 更新数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit updateData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("HrmJobActivities");
		sqlsKit.addData("jobactivityname", StringKit.null2Str(ob.getString("name")));	//名称
		sqlsKit.addData("jobgroupid", "83");											//固定使用1
		sqlsKit.setWhere("jobactivitymark", StringKit.null2Str(ob.getString("id")));	//编码
		sqlsKit.setOperateType(SqlOperateType.UPDATE);
		return sqlsKit;
	}
}
